package client;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import server.Message;
import server.MessageCodecFactory;

public class Client {
	private static final int	PORT	= 1234;

	public static void main(String[] args) throws IOException, InterruptedException {
		IoConnector connector = new NioSocketConnector();
		connector.getSessionConfig().setReadBufferSize(2048);

		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec", (IoFilter) new ProtocolCodecFilter(new MessageCodecFactory()));

		connector.setHandler(new ClientHandler(new Message("LOGIN:krim0957;karolyreka")));
		ConnectFuture future = connector.connect(new InetSocketAddress("192.168.56.1", PORT));

		if (!future.isConnected()) {
			return;
		}
		Thread.sleep(2000);
		IoSession session = future.getSession();
		session.write(new Message("LOGIN:kbim0960;kiss"));
		session.getConfig().setUseReadOperation(true);
		session.getCloseFuture().awaitUninterruptibly();

		System.out.println("After Writing");
		connector.dispose();
	}
}